package C003;

/**
 * ClassName: BinarySystem
 * Package: C003
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/11/26 16:54
 * @Version 1.0
 * @Since 1.0
 */
public class BinarySystem {
    static String separator = "--------------";
    public static void main(String[] args) {

        // 取反运算
        System.out.println(separator + "去反运算" + separator);
        operator1();
        System.out.println(separator + "相反数" + separator);
        operator2();
        System.out.println(separator + "位运算相关" + separator);
        operator3();
        System.out.println(separator);
        operator4();
        System.out.println(separator + "移位运算" + separator);
        operator5();
    }

    private static void operator5() {
        int num1 = 0b1111;
        printBinary(num1);
        printBinary(num1 << 1);
        printBinary(num1 << 2);
        System.out.println(separator);
        int num2 = 0b11100000;
        printBinary(num2);
        printBinary(num2 >> 1);
        printBinary(num2 >> 2);
        printBinary(num2 >>> 1);
        printBinary(num2 >>> 2);
        System.out.println(separator);
        int num3 = -1;
        printBinary(num3);
        printBinary(num3 >> 1);
        printBinary(num3 >> 2);
        printBinary(num3 >>> 1);
        printBinary(num3 >>> 2);
    }

    private static void operator4() {
        boolean test1 = returnTrue() | returnFalse();
        System.out.println("test1= " + test1);
        boolean test2 = returnTrue() || returnFalse();
        System.out.println("test2= " + test2);
        boolean test3 = returnFalse() & returnTrue();
        System.out.println("test3= " + test3);
        boolean test4 = returnFalse() && returnTrue();
        System.out.println("test4= " + test4);
    }

    private static void operator3() {
        int num1 = 0b00101;
        int num2 = 0b00011;
        printBinary(num1 & num2);
        printBinary(num1 | num2);
        printBinary(num1 ^ num2);
    }

    private static boolean returnTrue() {
        System.out.println("returnTrue执行");
        return true;
    }
    private static boolean returnFalse() {
        System.out.println("returnFalse执行");
        return false;
    }



    private static void operator2() {
        int num1 = 1;
        printBinary(num1);
        printBinary(~num1);
        printBinary(~num1 + 1);
        System.out.println(num1);
        System.out.println(~num1 + 1);
        // 最小值的相反数就是它自己
        int num2 = Integer.MIN_VALUE;
        printBinary(num2);
        printBinary(~num2);
        printBinary(~num2 + 1);
        System.out.println(num2);
        System.out.println(~num2 + 1);
    }

    private static void operator1() {
        int num1 = -1;
        printBinary(num1);
        printBinary(~num1);
        System.out.println(num1);
        System.out.println(~num1);
    }


    /**
     * 打印num的二进制形式
     * @param num
     */
    public static void printBinary(int num) {
        int midNum = 1;
        StringBuilder binaryStr = new StringBuilder();
        for (int i = 31; i >= 0; i--) {
            binaryStr.append((num & (midNum << i)) == 0 ? 0 : 1);
        }
        System.out.println(binaryStr);
    }
}
